package edu.umn.actvs;


import java.text.ParseException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import edu.umn.ftpmaster.Command;
import edu.umn.ftpmaster.Location_stp;

public class CreateEventActivity extends Activity{

	private EditText eventName;
	private EditText location;
	private EditText personName;
	private EditText datebox;
	private int day1, year1, month1;
	private Button createbtn, cancel;
	private DatePicker mPickDate;
	private Spinner hourSpinner;
	private Spinner minuteSpinner;
	private Spinner periodSpinner;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.createevent);
		eventName = (EditText)findViewById(R.id.editText1);
		location  = (EditText)findViewById(R.id.editText2);
		personName = (EditText)findViewById(R.id.personName);
		datebox = (EditText)findViewById(R.id.datebox);
		createbtn    = (Button)findViewById(R.id.button1);
		cancel    = (Button)findViewById(R.id.button2);

		hourSpinner = (Spinner) findViewById(R.id.hour_pick);
		ArrayAdapter hourAdapter = ArrayAdapter.createFromResource(
				this, R.array.hours, android.R.layout.simple_spinner_item);
		hourAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		hourSpinner.setAdapter(hourAdapter);

		minuteSpinner = (Spinner) findViewById(R.id.min_pick);
		ArrayAdapter minuteAdapter = ArrayAdapter.createFromResource(
				this, R.array.mins, android.R.layout.simple_spinner_item);
		minuteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		minuteSpinner.setAdapter(minuteAdapter);

		periodSpinner = (Spinner) findViewById(R.id.per_pick);
		ArrayAdapter periodAdapter = ArrayAdapter.createFromResource(
				this, R.array.period, android.R.layout.simple_spinner_item);
		minuteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		periodSpinner.setAdapter(periodAdapter);
		final EventDBCreator clientDB = new EventDBCreator(this);
		
		createbtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String eName = eventName.getText().toString();
				String address = location.getText().toString();
				String cpAddress = address;
				String day = Integer.toString(day1);
				String month = Integer.toString(month1);
				String year = Integer.toString(year1);
				String pName = personName.getText().toString();
				Log.w("name", eName );
				Log.w("location",address);
				Log.w("year", year);     
				Log.w("month", month);
				Log.w("day", day);
				Location_stp loc = new Location_stp();
				String date = datebox.getText().toString();
				String hour = String.valueOf(hourSpinner.getSelectedItem());
				String minutes = String.valueOf(minuteSpinner.getSelectedItem());
				String period = String.valueOf(periodSpinner.getSelectedItem());
				String[] dateparts = date.split("/");
				String dmonth = dateparts[0];
				String dday = dateparts[1];
				String dyear = dateparts[2];
				address=address.replace(' ', '+');
				address=address.concat("&sensor=true");
				address="address="+address;
				String gc = CommandHelper.sendGetRequest(address);
				String[] tokens = gc.split(":");
				if(tokens.length > 1){
					loc.latitude = Double.parseDouble(tokens[0]);
					loc.longitude = Double.parseDouble(tokens[1]);
					Log.w("Geocode: ", loc.latitude + ":" + loc.longitude);
				} else{
					Log.w("ERROR: bad geocode","line 93 create activity");
				}
				try {
					loc.time = CommandHelper.convertTime(dyear, dmonth, dday, hour, minutes, period);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				Command cmd = new Command("new", loc,eName,null,pName,cpAddress);
				try {
					Command resp = (Command)CommandHelper.sendCommand(Constants.SERVERIP, Constants.SERVERPORT, cmd);
					Log.w("Resp: ", resp.getMessage());
					clientDB.insertEvent(resp.getMessage());
					CharSequence text = "Congrats, Event created!";
					int duration = Toast.LENGTH_SHORT;
					Toast toast = Toast.makeText(getApplicationContext(), text, duration);
					toast.show();
					Intent intent = new Intent(CreateEventActivity.this, MainPanelActivity.class);
	
					startActivityForResult(intent,1);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//TODO create a Command to send to master
			}
		});
		
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});


	}
}
